from sqlalchemy.orm import Session

from apis_module.dao.reviews_dao import ReviewsDao
from backend_module.jsonvo.reviews_manage_vo import BackendReviewsListQueryModel
from backend_module.dao.reviews_manage_dao import ReviewsManageDAO
from utils import time_format_util


class ReviewsManageService:
    @classmethod
    def get_reviews_list_service(cls, query_db: Session, query_model: BackendReviewsListQueryModel):
        result_list = ReviewsManageDAO.get_reviews(query_db, query_model)
        if result_list:
            result_list = time_format_util.list_format_datetime(result_list)

            # 分页操作
            start = (query_model.page - 1) * query_model.pageSize
            end = query_model.page * query_model.pageSize
            page_results: list = result_list[start:end]
            total = len(result_list)

            results_paginated = {
                "total": total,
                "items": page_results,
            }

            return results_paginated
        else:
            return None

    @classmethod
    def get_reviews_detail_service(cls, db: Session, reviews_id: str):
        result = ReviewsDao.get_reviews_by_id(db, reviews_id)
        return result
